package com.tuoyu.peopletest.model;



import com.tuoyu.peopletest.entity.UserBean;

import org.litepal.LitePal;

import java.util.List;


/**
 * 用户数据
 */
public class UserModel {
    private final static String TAG = "UserModel";

    private static UserModel sInstance;

    private UserModel() {

    }

    public static UserModel get() {
        if (sInstance == null) {
            sInstance = new UserModel();
        }
        return sInstance;
    }

    public void save(UserBean userBean) {
        userBean.save();
    }

    public List<UserBean> getBeanByNameOrPhone(String text) {
        List<UserBean> list01 = LitePal.where(" user_name like ? ", "%" + text + "%")
                .find(UserBean.class);
        if (list01.size() > 0) {
            return list01;
        }

        List<UserBean> list02 = LitePal.where(" phone_number like ? ", "%" + text + "%")
                .find(UserBean.class);
        return list02;
    }

    public List<UserBean> getBeanByNameOrBirDay(String text) {
        List<UserBean> list01 = LitePal.where(" user_name like ? ", "%" + text + "%")
                .find(UserBean.class);
        if (list01.size() > 0) {
            return list01;
        }

        List<UserBean> list02 = LitePal.where(" birth_date like ? ", "%" + text + "%")
                .find(UserBean.class);
        return list02;
    }
    public UserBean getBeanBy_UserId(String userId) {
        List<UserBean> list01 = LitePal.where(" user_id_local = ? ", userId)
                .find(UserBean.class);
        if (list01.size() > 0) {
            return list01.get(0);
        }
        return null;
    }

    public UserBean getBeanOnly(String name, String phone) {

        List<UserBean> list = LitePal.where(" user_name = ? and phone_number = ? ",
                name, phone)
                .find(UserBean.class);
        return list.size() > 0 ? list.get(0) : null;
    }

    public UserBean getBeanOnlyByName_birDay(String name,String birDay) {

        List<UserBean> list = LitePal.where(" user_name = ? and birth_date = ? ",
                name,birDay)
                .find(UserBean.class);
        return list.size() > 0 ? list.get(0) : null;
    }

    public List<UserBean> getList(int page, int pageMax) {
        int limit = (page - 1) * pageMax;
        return LitePal
                .order("time desc")
                .limit(pageMax)
                .offset(limit)
                .find(UserBean.class);
    }


    public List<UserBean> getListByNameOrPhone(String text) {
        List<UserBean> userBeans = LitePal.where("user_name = ? ", text)
                .find(UserBean.class);
        if(userBeans.size()>0){
            return userBeans;
        }else {
            userBeans = LitePal.where("phone_number = ? ", text)
                    .find(UserBean.class);
            return userBeans;
        }
    }

    public void delectBeanByUse_localId(String user_id_local) {
        LitePal.deleteAll(UserBean.class,"user_id_local = ? ",user_id_local);
    }
}
